package com.xdtech.transportation.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xdtech.core.model.BaseItem;
import com.xdtech.core.orm.Page;
import com.xdtech.core.orm.utils.BeanUtils;
import com.xdtech.transportation.dao.TranDriverDao;
import com.xdtech.transportation.model.TranDriver;
import com.xdtech.transportation.service.TranDriverService;
import com.xdtech.transportation.vo.TranDriverItem;
import com.xdtech.web.model.Pagination;

/**
 * 
 * @author max.zheng
 * @create 2015-03-17 14:04:31
 * @since 1.0
 * @see
 */
@Service
public class TranDriverServiceImpl implements TranDriverService {
	@Autowired
	private TranDriverDao tranDriverDao;

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-03-17 14:04:31
	 * @modified by
	 * @param entity
	 */
	public void save(TranDriver entity) {
		tranDriverDao.save(entity);
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-03-17 14:04:31
	 * @modified by
	 * @param entity
	 */
	public void saveAll(List<TranDriver> entities) {
		tranDriverDao.saveAll(entities);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-03-17 14:04:31
	 * @modified by
	 * @param entity
	 */
	public void delete(TranDriver entity) {
		tranDriverDao.delete(entity);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-03-17 14:04:31
	 * @modified by
	 * @param id
	 */
	public void delete(Long id) {
		tranDriverDao.delete(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-03-17 14:04:31
	 * @modified by
	 * @param id
	 * @return
	 */
	public TranDriver get(Long id) {
		return tranDriverDao.get(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-03-17 14:04:31
	 * @modified by
	 * @return
	 */
	public List<TranDriver> getAll() {
		return tranDriverDao.getAll();
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-03-17 14:04:31
	 * @modified by
	 * @param pg
	 * @param values
	 * @return
	 */
	public Map<String, Object> loadPageAndCondition(Pagination pg,
			Map<String, String> values) {
		Map<String, Object> results = new HashMap<String, Object>();
		List<Object> tranDrivers = null;
		long rows = 0;
		if (pg!=null) {
			Page<TranDriver> page = tranDriverDao.findPage(pg,"from TranDriver order by createTime desc", values);
			tranDrivers = BeanUtils.copyListProperties(
					TranDriverItem.class, page.getResult());
			rows = page.getTotalItems();
		}else {
			List<TranDriver> tranDriverList = tranDriverDao.find("from TranDriver order by id desc", values);
			tranDrivers = BeanUtils.copyListProperties(
					TranDriverItem.class, tranDriverList);
			rows = tranDrivers.size();
		}
		results.put("total", rows);
		results.put("rows", tranDrivers);
		return results;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-03-17 14:04:31
	 * @modified by
	 * @param item
	 * @return
	 */
	public boolean saveOrUpdateTranDriver(TranDriverItem item) {
		TranDriver tranDriver = null;
		if (item.getId()==null) {
			tranDriver = new TranDriver();
		}else {
			tranDriver = tranDriverDao.get(item.getId());
		}
		//澶嶅埗鍓嶅彴淇敼鐨勫睘鎬�
		BeanUtils.copyProperties(tranDriver, item);
		tranDriverDao.save(tranDriver);
		return true;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-03-17 14:04:31
	 * @modified by
	 * @param tranDriverId
	 * @return
	 */
	public TranDriverItem loadTranDriverItem(Long tranDriverId) {
		TranDriver tranDriver = tranDriverDao.get(tranDriverId);
		TranDriverItem tranDriverItem = new TranDriverItem();
		BeanUtils.copyProperties(tranDriverItem, tranDriver);
		return tranDriverItem;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-03-17 14:04:31
	 * @modified by
	 * @param id
	 * @return
	 */
	public boolean deleteTranDriverInfo(long id) {
		delete(id);
		return true;
	}
	
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-03-17 14:04:31
	 * @modified by
	 * @param newIds
	 * @return
	 */
	public boolean deleteTranDriverInfo(List<Long> tranDriverIds) {
		for (Long id : tranDriverIds) {
			delete(id);
		}
		return true;
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-4-18下午12:25:40
	 * @modified by
	 * @param pg
	 * @param baseItem
	 * @param queryName
	 * @return
	 */
	@Override
	public Map<String, Object> loadPageDataByConditions(Pagination pg,
			BaseItem baseItem, String queryName) {
		// TODO Auto-generated method stub
		return null;
	}

}
